/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#ifndef Capturer_H_
#define Capturer_H_

#include "../Utils/CppExtensions.h"
#include "../Utils/BitManipulation.h"
#include "../CallbackServers/CallbackConnector.h"

namespace Timers
{

class Capturer16 : public CallbackConnector
{
public:
	Capturer16(uint8_t callbackVectorNumber, IOAddress16 icr, IOAddress tccrb, IOAddress timsk, uint8_t icie, IOAddress tifr, uint8_t icf);

	virtual ~Capturer16();

	inline void setICRValue(uint16_t value) FORCE_INLINE;
	inline uint16_t readICR() FORCE_INLINE;

	inline void enableNoiseCanceler() FORCE_INLINE;
	inline void disableNoiseCanceler() FORCE_INLINE;

	inline void triggerOnRisingEdge() FORCE_INLINE;
	inline void triggerOnFallingEdge() FORCE_INLINE;

	inline void enableICRInterrupts() FORCE_INLINE;
	inline void disableICRInterrupts() FORCE_INLINE;
	inline bool readICRFlag() FORCE_INLINE;
	inline void clearICRFlag() FORCE_INLINE;

protected:
	IOAddress16 icr;
	IOAddress tccrb;
	IOAddress timsk;
	uint8_t icie;
	IOAddress tifr;
	uint8_t icf;
};


void Capturer16::setICRValue(uint16_t value)
{
	*icr = value;
}

uint16_t Capturer16::readICR()
{
	return *icr;
}

void Capturer16::enableNoiseCanceler()
{
	setBit(*tccrb, ICNC1);

}
void Capturer16::disableNoiseCanceler()
{
	clearBit(*tccrb, ICNC1);
}

void Capturer16::triggerOnRisingEdge()
{
	setBit(*tccrb, ICES1);
}

void Capturer16::triggerOnFallingEdge()
{
	clearBit(*tccrb, ICES1);
}

void Capturer16::enableICRInterrupts()
{

	setBit(*timsk, icie);

}
void Capturer16::disableICRInterrupts()
{
	clearBit(*timsk, icie);
}

bool Capturer16::readICRFlag()
{
	return bitIsSetInRegister(*tifr, icf);
}

void Capturer16::clearICRFlag()
{
	setBit(*tifr, icf);
}

}

#endif /* Capturer_H_ */
